package com.cooper.android.comm;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class ReflectionUtils {

	public static final String HAN_W_REGULAR = "[A-Za-z0-9\u4e00-\u9fa5]";

	public static final String NOT_HAN_W_WREGULAR = "[^A-Za-z0-9\u4e00-\u9fa5]";

	public static String fieldToJavaSet(String field) {
		String reg = "_([a-z])|^([a-z])";
		Pattern p = Pattern.compile(reg);
		Matcher m = p.matcher(field.toLowerCase());
		StringBuffer result = new StringBuffer();
		while (m.find()) {
			String head = m.group(1) == null ? m.group(2) : m.group(1);
			m.appendReplacement(result, head.toUpperCase());
		}
		m.appendTail(result);
		return "set" + result.toString();
	}

	public static String strToRegular(String value) {
		return value.replace("\\", "\\\\").replace(".", "\\.")
				.replace("*", "\\*").replace("$", "\\$").replace("(", "\\(")
				.replace(")", "\\)").replace("+", "\\+").replace("[", "\\[")
				.replace("]", "\\]").replace("?", "\\?").replace("/", "\\/")
				.replace("^", "\\^").replace("{", "\\{").replace("}", "\\}")
				.replace("|", "\\|");
	}

	public static String stringToFileName(String url) {

		String result = url.replace("http://", "c").replaceAll(
				"[\\s\\.\\\\/:\\(\\)@#&$\\-\\[\\]%\'`^!*\"<>|]", "_");
		if (url.length() >= 256) {
			return result.substring(0, 255);
		} else
			return result;
	}

	public static String escapeRuleExpString(String src) {
		return src.replace("\\", "\\\\").replace("/", "\\/")
				.replace("$", "\\$").replace("(", "\\(").replace(")", "\\)")
				.replace("*", "\\*").replace("+", "\\+").replace(".", "\\.")
				.replace("[", "\\[").replace("]", "\\]").replace("?", "\\?")
				.replace("^", "\\^").replace("{", "\\{").replace("}", "\\}")
				.replace("|", "\\|");
	}
}
